function winner=win(state,player)
    BOARD_SIZE=3;
    
    temp=state;
    
    for(i=1:BOARD_SIZE)
        totalrow=0;
        for(j=1:BOARD_SIZE)
            if(~temp(i,j)==0)
                totalrow=totalrow+temp(i,j);
            else
                totalrow=0;
                break;
            end
        end
        if(totalrow==BOARD_SIZE*player);
            winner=player;
            return;
        end
    end
    
    temp=state';
    
    for(i=1:BOARD_SIZE)
        totalrow=0;
        for(j=1:BOARD_SIZE)
            if(~temp(i,j)==0)
                totalrow=totalrow+temp(i,j);
            else
                totalrow=0;
                break;
            end
        end
        if(totalrow==BOARD_SIZE*player);
            winner=player;
            return;
        end
    end
    
    d1=[state(1,1),state(2,2),state(3,3)];
    d2=[state(1,3),state(2,2),state(3,1)];
    
    if( (isempty(find(d1==0)) && sum(d1)==BOARD_SIZE*player ) || (isempty(find(d2==0)) && sum(d2)==BOARD_SIZE*player ))
        winner=player;
        return;
    end
    
    winner=0;
end

